﻿#include "DirHelper.h"

#include "log/TSpdlog.h"
#include "../common/QtAppMacro.h"

#include <QDir>
#include <QDebug>
#include <QVector>
#include <QFileInfoList>
#include <QStandardPaths>
#include <QSettings>
#include <QCoreApplication>

#ifdef WIN32
#include <Windows.h>
#endif
#include <algorithm>
#include <iostream>

DirHelper * DirHelper::m_instance = nullptr;
DirHelper *DirHelper::getInstance()
{
    if (!m_instance) {
        m_instance = new DirHelper;
    }
    return m_instance;
}

void DirHelper::createDir()
{
    if (!QFileInfo::exists(m_mainPath)) {
        if (!QDir().mkpath(m_mainPath) ) {
            APP_ASSERT(false,"create dir error","main path");
        }
    }

    if (QFileInfo::exists(m_mainPath)) {
        QDir dir(m_mainPath);
        if (!dir.exists(log)) {
            if (!dir.mkdir(log)) {
                APP_ASSERT(false,"create dir error","log path");
            }
        }

        if (!dir.exists(config)){
            if (!dir.mkdir(config))
                APP_ASSERT(false,"create dir error","config path");
        }

        if (!dir.exists(dump)) {
            if (!dir.mkdir(dump))
                APP_ASSERT(false,"create dir error","dump path");
        }

        if (!dir.exists(download)) {
            if (!dir.mkdir(download))
                APP_ASSERT(false,"create dir error","db path");
        }
    } else {
        APP_ASSERT(false,"create dir error","main path");
    }
}

DirHelper::DirHelper(QObject *parent) : QObject(parent)
{
    /* 同一组织下的多个app，每个对应自己的数据存储区域，应使application不同，安装默认为用户目录（一般位于C盘），当空间不够时，
     * 改变为其他盘符 */
    QSettings setting(APP_COMPANY_NAME,APP_COMPANY_APP_NAME);

    /* 关于目录说明 */
    QString app_data = setting.value(REG_QT_APP_DATA,"none").toString();
    if (app_data == "none") {
        /* C:/Users/sl/AppData/Local/company/app-project 必须先设置全局app */
        m_mainPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
        setting.setValue(REG_QT_APP_DATA,m_mainPath);
    } else {
        m_mainPath = app_data;
    }
    createDir();
}

bool DirHelper::changeDirPath(const QString &path)
{
#ifdef WIN32
    QString newPath = path;
    newPath = newPath.remove("file:///");
#else
    QString newPath = path;
    newPath = newPath.remove("file://");
#endif
    if (newPath.contains(QRegExp("[\\x4e00-\\x9fa5]+"))) {
        return false;
    } else {
        QSettings setting(APP_COMPANY_NAME,APP_COMPANY_APP_NAME);
        /* 检查path合法性 */
        if (m_mainPath == newPath + APP_COMPANY_NAME)
            return true;

        setting.setValue(REG_QT_APP_DATA,newPath + APP_COMPANY_NAME);
        m_mainPath = setting.value(REG_QT_APP_DATA).toString();
        createDir();
        return true;
    }
}

void DirHelper::resetMainPath()
{
    QSettings setting(APP_COMPANY_NAME,APP_COMPANY_APP_NAME);
    m_mainPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
    setting.setValue(REG_QT_APP_DATA,m_mainPath);
    createDir();
}

QString DirHelper::getCurrentDirPath() const
{
    return m_mainPath;
}

QString DirHelper::configPath()
{
    return m_mainPath + "/" + this->config;
}

QString DirHelper::logPath()
{
    return m_mainPath + "/" + this->log;
}


QString DirHelper::dumpPath()
{
    return m_mainPath + "/" + this->dump;
}

QString DirHelper::downloadPath()
{
    return m_mainPath+"/" + this->download;
}
QString DirHelper::updatePath()
{
    return m_mainPath+"/"+this->update;
}

QString DirHelper::getAppPath()
{
    return QCoreApplication::applicationDirPath();
}

QString DirHelper::toPlatformPath(const QString &path)
{
    return QDir::toNativeSeparators(path);
}
